﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class ReadFile
    { /*
        public static Tuple<int, int, bool, bool, Dictionary<string, string>, int, int> ReadAllFile()
        {
            Dictionary<string, string> m_allDocs = new Dictionary<string, string>(); //Dictionary<fileName , all text>
            string path = Directory.GetCurrentDirectory(); // get the directory where the EXE/Application file is
            path += "\\Files"; // add the folder name "Files" to the path
            string[] allFiles = Directory.GetFiles(path); // get all files from the path
            bool isSteam = false;
            bool repeatNGrams = false;
            int k = 0;
            int n = 0;
            int tops = 0;
            int numDocsFromEachCluster = 0;
            int numOfPairsForExpert = 0;

            System.Xml.XmlDocument Xmldoc = new System.Xml.XmlDocument();

            foreach (string currFile in allFiles) // for each of the files
            {
                string fileName = currFile.Split('\\').Last();
                //  StreamReader sr = new StreamReader(currFile); // open a new StreamReader

                if (fileName == "stopWords.txt")//if file: "stopWords.txt"
                    continue;
                if (fileName == "config.txt")//if file: "config.txt", file structure: k,n,isStream?,doSplit?
                {
                    StreamReader sr = new StreamReader(currFile); // open a new StreamReader
                    string line = sr.ReadLine(); // Read a line 
                    string[] split = line.Split(new char[] { ',' }); // split the line and get the Doc Number
                    if (split.Length != 7) // cheks if input data length in config file is correct
                    {
                        Console.WriteLine("Configuration file is lack of data - Programg will be terminated");
                        sr.Close();
                        return null;
                    }
                    k = int.Parse(split[0]);
                    n = int.Parse(split[1]);
                    tops = int.Parse(split[4]);
                    numDocsFromEachCluster = int.Parse(split[5]);
                    //numOfPairsForExpert = int.Parse(split[6]);
                    if (split[2] == "YES")
                        isSteam = true;
                    if (split[3] == "YES")
                        repeatNGrams = true;
                    sr.Close();
                }
                else
                {
                    XMLValidator v = new XMLValidator(currFile);
                    if (!(v.Validate())) // is file valide
                        v.FixFile(currFile);

                    Xmldoc.Load(currFile);
                    System.Xml.XmlElement root = Xmldoc.DocumentElement;
                    System.Xml.XmlNodeList docNo = root.GetElementsByTagName("DOCNO");

                    System.Xml.XmlNodeList headline = root.GetElementsByTagName("HEADLINE");
                    System.Xml.XmlNodeList text = root.GetElementsByTagName("TEXT");
                    for (int j = 0; j < text.Count; j++)
                    {
                        string docName = docNo.Item(j).InnerText;
                        string docText = text.Item(j).InnerText;
                        
                        string[] wordCount = docText.Split(' ');
                        if (wordCount.Length < (n + k))// if file Length is less then (n+k), then skeep this file
                        {
                            Console.WriteLine("The file: -" + docName + "- won't be processed since it doesn't has enough data (length) of text in it.");
                            continue;
                        }
                        docName = docName.TrimEnd(' ');
                        docName = docName.TrimStart(' ');
                        m_allDocs.Add(docName, docText); //adds all text from the file with key = Doc Name
                    }
                }

            }

            return new Tuple<int, int, bool, bool, Dictionary<string, string>, int, int>(k, n, isSteam, repeatNGrams, m_allDocs, tops, numDocsFromEachCluster);
        } */

        /*
         * BACKUP before Boris's copy
         */
        public static Tuple<int,int,bool, bool, Dictionary<string, string>,int, int> ReadAllFile()
        {
            Dictionary<string,string> m_allDocs = new Dictionary<string, string>(); //Dictionary<fileName , all text>
            string path = Directory.GetCurrentDirectory(); // get the directory where the EXE/Application file is
            path += "\\Files"; // add the folder name "Files" to the path
            string[] files = Directory.GetFiles(path); // get all files from the path
            bool isSteam = false;
            bool repeatNGrams = false;
            int k = 0;
            int n = 0;
            int tops = 0;
            int chosenDocsFromClusters = 0;

            foreach (string file in files) // for each of the files
            {
                string fileName = file.Split('\\').Last();
                StreamReader sr = new StreamReader(file); // open a new StreamReader

                if (fileName == "stopWords.txt")//if file: "stopWords.txt"
                    continue;
                if (fileName == "config.txt")//if file: "config.txt", file structure: k,n,isStream?,doSplit?
                {                  
                    string line = sr.ReadLine(); // Read a line 
                    string[] split = line.Split(new char[] { ',' }); // split the line and get the Doc Number
                    if (split.Length != 7) // cheks if input data length in config file is correct
                    {
                        Console.WriteLine("Configuration file is lack of data - Programg will be terminated");
                        return null;
                    }
                    k = int.Parse(split[0]);
                    n = int.Parse(split[1]);
                    tops = int.Parse(split[4]);
                    chosenDocsFromClusters = int.Parse(split[5]);
                    if (split[2] == "YES")
                        isSteam = true;
                    if (split[3] == "YES")
                        repeatNGrams = true;
                }
                else
                {
                    // TODO - the program crash now when the file has less word the n+k
                    //if (fileLength < (n + k))
                    //{
                    //    Console.WriteLine("the file: -" + fileName + "- won't be processed because is has enough data of text in it.");
                    //    continue;
                    //}
                    m_allDocs.Add(fileName, sr.ReadToEnd()); //adds all text from the file with key = Doc Name
                    
                }
                sr.Close();
            }

            return new Tuple<int, int, bool, bool, Dictionary<string, string>, int, int>(k, n, isSteam, repeatNGrams, m_allDocs, tops, chosenDocsFromClusters); 
        }
        
    }
}
